/* !点击logo回到首页 */
$('.logoimg').on('click', function () {
    location.href='../html/huaindex.html'
})
/* !账号登录与微信扫码登录的切换 */
$('.login_type p').on('click', function () {
    $(this).addClass('active').siblings().removeClass('active');
    $('.login_form form').eq($(this).index()).css({
        display: 'block',
    }).siblings().css({
        display: 'none',
    })
});

/* !账号短信登录切换 */
    $('#form1 div .npturn').on('click', function () {
    $(this).parent().parent().css({
        display: "none"
    });
    $(this).parent().parent().siblings().css({
        display:"block"
    })
    })
/* !登录验证 */
/* !验证电话和邮箱 */
$.validator.addMethod('teltest', function (value) {
    let reg = /^(1[3-9]\d{9}|[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+)$/;
    if (reg.test(value)) {
        return true;
    }
    return false;
}, '请输入正确的邮箱账号或手机号码');
/* !验证密码 8-14个字符 数字 字母 标点标点符号 组成 至少有两种符号组成 */
$.validator.addMethod("pawdtest", function (value) {
    let reg = /^(?![a-z]+$)(?![\d]+$)(?![!@#$%^&*<?\.]+$)[\da-z\.*&,?@#$%^-]{6,10}$/i;
    if (reg.test(value)) {
        return true;
    }
    return false;
}, '6-10个字符组成，不能全为数字字符符号');

// 验证验证码
function createCode(code) {
            let c = '';
            for (let i = 1; i <= code; i++) {
                c += parseInt(Math.random() * 10);
            }
            return c;
        }
$('.yzm').on('click', function () {
            $('.yzm').html(createCode(6));
        })
 $.validator.addMethod('msgtest', function (value) {
     if (value == $('.yzm').html()) {
        return true;
    }
    return false;
 }, '验证码错误');

let phonenum = document.querySelector('#phonenum');
let password = document.querySelector('#password');
$('#form1').validate({
    rules: {
        tel: {
            required: true,
            teltest: true,            
        },
        pawd: {
            required: true,
            pawdtest: true,
        },
        msg: {
            required:true,
            msgtest:true,
        }
    },
    messages: {
        tel: {
            required: '请输入您的邮箱账号或手机号码',
        },
        pawd: {
            required: '请输入正确的密码',
        },
        msg: {
            required: '请输入验证码',
        }
    },
    submitHandler: function (form) {
        let p = pAjax({
            url: '/flo/login.php',
            type: 'post',
            data: {
                phonenum: phonenum.value,
                password: password.value,
            },
        });
        p.then((res) => {
            res = JSON.parse(res);
            if (res.code) {
                setCookie('login', phonenum.value);
                let url = localStorage.getItem("url");
                localStorage.removeItem('url');
                location.href = url ? url : '../html/huaindex.html';
            } else {
                confirm('用户名或密码不存在,请先注册') ? location.href = '../html/regist.html' : alert('用户名或密码错误');
            }
            
        })
    },
});

        